<?php

class auction_model extends jqxGrid_CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    
    function get($id)
    {
        $query = $this->db->query("
            SELECT * 
            FROM `auction` 
            WHERE `delete` is null
                AND `id`='$id'
        ");
        return $query->result();
    }
    function delete($id){
        $query = "
            UPDATE `auction` 
            SET `delete` = now()
            WHERE `id`='$id'
        ";
        $this->db->query($query);
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
        
    }
    
    function restore($id){
        $query = "
            UPDATE `auction` 
            SET     `delete`    =   null
            WHERE   `id`        =   '$id'
        ";
        $this->db->query($query);
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    
    function changeStatus($id,$status){
        $query = "
            UPDATE `auction` 
            SET `status`    =   '$status',
                `update`    =   now()
            WHERE `id`='$id'
        ";
        $this->db->query($query);
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    
    function insert($params){
        $this->db->set('insert', 'NOW()', FALSE);
        $this->db->insert('auction', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function update($id,$params){
        $this->db->where(array('id'=> $id,'delete'=> null));
        //$this->db->where('delete', 'is null',FALSE);
        $this->db->set('update', 'NOW()', FALSE);
        $this->db->update('auction', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function gettotal_rows($id){
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS `product`.`name` as 'productname',`auction`.* 
            FROM `auction`,`product`
                WHERE `product`.`id`=`auction`.`product`
                AND `auction`.`product`='$id'
                    ";
        $fields=array(
            "productname"=>"`product`.`name`"
        );
        $this->__new($strQuery,"",$fields);
        return $this->jqxBinding();
//        $FstrSQL="SELECT SQL_CALC_FOUND_ROWS * FROM `auction`";
//        $where=" WHERE `product`='$id' ";
//        
//        $pagenum = isset($_GET['pagenum'])?$_GET['pagenum']:0;
//	$pagesize = isset($_GET['pagesize'])?$_GET['pagesize']:20;
//	$start = $pagenum * $pagesize;
//	
//        $SQLquery = "$FstrSQL $where";
//	
//	$query=$this->db->query($SQLquery);
//        $sql = "SELECT FOUND_ROWS() AS `found_rows`;";
//	$query=$this->db->query($sql);
//        //$query->result();
//        $rows=$query->row_array();
//	$total_rows = $rows['found_rows'];
//        $result['total_rows']=$total_rows;
//	$filterquery = "";
//        
//        $SQLquery = "$FstrSQL $where  LIMIT $start, $pagesize";
//        
//        // filter data.
//	if (isset($_GET['filterscount']))
//	{
//		$filterscount = $_GET['filterscount'];
//		
//		if ($filterscount > 0)
//		{
//			$where.= " AND (";
//			$tmpdatafield = "";
//			$tmpfilteroperator = "";
//			for ($i=0; $i < $filterscount; $i++)
//		    {
//				// get the filter's value.
//				$filtervalue = $_GET["filtervalue" . $i];
//				// get the filter's condition.
//				$filtercondition = $_GET["filtercondition" . $i];
//				// get the filter's column.
//				$filterdatafield = $_GET["filterdatafield" . $i];
//				// get the filter's operator.
//				$filteroperator = $_GET["filteroperator" . $i];
//				
//                                //check filterdatafield
//                                if($filterdatafield[0] === "_" && $filterdatafield[strlen($filterdatafield) - 1] === "_"){
//                                    $filterdatafield=  substr($filterdatafield, 1, -1);
//                                }
//				if ($tmpdatafield == "")
//				{
//					$tmpdatafield = $filterdatafield;			
//				}
//				else if ($tmpdatafield <> $filterdatafield)
//				{
//					$where .= ")AND(";
//				}
//				else if ($tmpdatafield == $filterdatafield)
//				{
//					if ($tmpfilteroperator == 0)
//					{
//						$where .= " AND ";
//					}
//					else $where .= " OR ";	
//				}
//				
//				// build the "WHERE" clause depending on the filter's condition, value and datafield.
//                                // possible conditions for string filter: 
//                                //      'EMPTY', 'NOT_EMPTY', 'CONTAINS', 'CONTAINS_CASE_SENSITIVE',
//                                //      'DOES_NOT_CONTAIN', 'DOES_NOT_CONTAIN_CASE_SENSITIVE', 
//                                //      'STARTS_WITH', 'STARTS_WITH_CASE_SENSITIVE',
//                                //      'ENDS_WITH', 'ENDS_WITH_CASE_SENSITIVE', 'EQUAL', 
//                                //      'EQUAL_CASE_SENSITIVE', 'NULL', 'NOT_NULL'
//                                // 
//                                // possible conditions for numeric filter: 'EQUAL', 'NOT_EQUAL', 'LESS_THAN',
//                                //  'LESS_THAN_OR_EQUAL', 'GREATER_THAN', 'GREATER_THAN_OR_EQUAL', 
//                                //  'NULL', 'NOT_NULL'
//                                //  
//                                // possible conditions for date filter: 'EQUAL', 'NOT_EQUAL', 'LESS_THAN', 
//                                // 'LESS_THAN_OR_EQUAL', 'GREATER_THAN', 'GREATER_THAN_OR_EQUAL', 'NULL', 
//                                // 'NOT_NULL'                         
//                                
//                                $vowels = array( 
//                                    "đ"=>"d", 
//                                    "'"=>"",
//                                    "\\"=>"",
//                                    "/"=>""
//                                    );
//                                
//                                $filtervalue = str_replace(
//                                        array_keys($vowels),  
//                                        array_values($vowels), 
//                                        $filtervalue
//                                        );
//
//				switch($filtercondition)
//				{
//                                        case "NULL":
//                                            $where .= " (`$filterdatafield` is null)";
//                                            break;
//                                        case "EMPTY":
//                                                $where .= " (`$filterdatafield` is null) or (`$filterdatafield`='')";
//                                                break;
//                                        case "NOT_NULL":
//                                            $where .= " (`$filterdatafield` is not null)";
//                                            break;
//                                        case "NOT_EMPTY":
//                                                $where .= " (`$filterdatafield` is not null) and (`$filterdatafield` <>'')";
//                                                break;
//                                        case "CONTAINS_CASE_SENSITIVE":
//                                        case "CONTAINS":
//                                                $where .= " `$filterdatafield` LIKE '%$filtervalue%'";
//                                                break;
//                                        case "DOES_NOT_CONTAIN_CASE_SENSITIVE":
//                                        case "DOES_NOT_CONTAIN":
//                                                $where .= " `$filterdatafield` NOT LIKE '%$filtervalue%'";
//                                                break;
//                                        case "EQUAL_CASE_SENSITIVE":
//                                        case "EQUAL":
//                                            $where .= " `$filterdatafield` = '$filtervalue'";
//                                            break;
//                                        case "NOT_EQUAL":
//                                            $where .= " `$filterdatafield` <> '$filtervalue'";
//                                            break;
//                                        case "GREATER_THAN":
//                                            $where .= " `$filterdatafield` > '$filtervalue'";
//                                            break;
//                                        case "LESS_THAN":
//                                            $where .= " `$filterdatafield` < '$filtervalue'";
//                                            break;
//                                        case "GREATER_THAN_OR_EQUAL":
//                                            $where .= " `$filterdatafield` >= '$filtervalue'";
//                                            break;
//                                        case "LESS_THAN_OR_EQUAL":
//                                            $where .= " `$filterdatafield` <= '$filtervalue'";
//                                            break;
//                                        case "STARTS_WITH_CASE_SENSITIVE":
//                                        case "STARTS_WITH":
//                                            $where .= " `$filterdatafield` LIKE '$filtervalue%'";
//                                            break;
//                                        case "ENDS_WITH_CASE_SENSITIVE":
//                                        case "ENDS_WITH":
//                                            $where .= " `$filterdatafield` LIKE '%$filtervalue'";
//                                            break;
//                                        
//                                        
//				}
//								
//				if ($i == $filterscount - 1)
//				{
//					$where .= ")";
//				}
//				
//				$tmpfilteroperator = $filteroperator;
//				$tmpdatafield = $filterdatafield;			
//			}
//			// build the query.
//                        $SQLquery = "$FstrSQL ".$where;
//			$filterquery = $SQLquery;
//			//$result = mysql_query($query) or die("SQL Error 1: " . mysql_error());
//			$query=$this->db->query($SQLquery);
//                        $sql = "SELECT FOUND_ROWS() AS `found_rows`;";
//			//$rows = mysql_query($sql);
//			//$rows = mysql_fetch_assoc($rows);
//                        $query=$this->db->query($sql);
//                        $rows=$query->row_array();
//                        $total_rows = $rows['found_rows'];
//                        $result['total_rows']=$total_rows;
//			$new_total_rows = $rows['found_rows'];		
//			$SQLquery = "$FstrSQL ".$where." LIMIT $start, $pagesize";		
//			$total_rows = $new_total_rows;	
//		}
//	}
//	
//	if (isset($_GET['sortdatafield']))
//	{
//                $sortfield = $_GET['sortdatafield'];
//                //fix sortfield
//                if($sortfield[0] === "_" && $sortfield[strlen($sortfield) - 1] === "_"){
//                    $sortfield=  substr($sortfield, 1, -1);
//                }
//                
//		$sortorder = $_GET['sortorder'];
//	
////                if ($_GET['filterscount'] == 0)
////		{
//			if ($sortorder == "desc")
//			{
//				$SQLquery = "$FstrSQL $where ORDER BY `$sortfield` DESC LIMIT $start, $pagesize";
//			}
//			else if ($sortorder == "asc")
//			{
//				$SQLquery = "$FstrSQL $where ORDER BY `$sortfield` ASC LIMIT $start, $pagesize";
//			}
////		}
////		else
////		{
////			if ($sortorder == "desc")
////			{
////				$filterquery .= " ORDER BY `$sortfield` DESC LIMIT $start, $pagesize";
////			}
////			else if ($sortorder == "asc")	
////			{
////				$filterquery .= " ORDER BY `$sortfield` ASC LIMIT $start, $pagesize";
////			}
////			$SQLquery = $filterquery;
////		}		
//	}
//        
//        $query=$this->db->query($SQLquery);
//        $result['rows']= $query->result();
//        
//        return $result;
//        //$data['total_rows']=$total_rows;
//        
    }
    
    
}
?>
